import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
import 'package:gksz/political_Theory/Recommend/MessagePage/MessagePage.dart';
import 'package:gksz/political_Theory/Video/Video.dart';
import 'package:gksz/political_Theory/Video/VideoPage.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:shared_preferences/shared_preferences.dart';

Dio dio = new Dio();

class Shoucang extends StatefulWidget {
  Shoucang({Key key}) : super(key: key);
  _ShoucangState createState() => _ShoucangState();
}

class _ShoucangState extends State<Shoucang> {
  var user_id;
  var number;
  var cang_video;
  var cang_news;
  getName() async {
    //获取Id
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    user_id = sharedPreferences.getString('user_id');

    number = sharedPreferences.getString('number');
    shoucang(); // 初始化根据ID拉取收藏数据
    video();
    setState(() {});
  }

  void shoucang() async {
    //收藏
    if (number.length == 10) {
      Response response = await dio.get(
          'http://xssw.gdit.edu.cn/sizheng/api/v1/collect?query={"studentId":"${user_id}"}&populate={"path":"news"}');
      cang_news = response.data;
    } else {
      Response response = await dio.get(
          'http://xssw.gdit.edu.cn/sizheng/api/v1/collect?query={"teacherId":"${user_id}"}&populate={"path":"news"}');
      cang_news = response.data;
    }
    // print(cang_news[0]["news"]);
    setState(() {});
  }

  void video() async {
    //收藏
    if (number.length == 10) {
      Response response = await dio.get(
          'http://xssw.gdit.edu.cn/sizheng/api/v1/collect?query={"studentId":"${user_id}"}&populate={"path":"video"}');
      cang_video = response.data;
    } else {
      Response response = await dio.get(
          'http://xssw.gdit.edu.cn/sizheng/api/v1/collect?query={"teacherId":"${user_id}"}&populate={"path":"video"}');
      cang_video = response.data;
    }
    // print(cang_news[0]["news"]);
    setState(() {});
  }

  @override
  void initState() {
    super.initState();
    getName();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('收藏'),
        ),
        body: ListView(
          children: <Widget>[
            ExpansionTile(
              title: Text("要闻、推荐、学习"),
              backgroundColor: Colors.white,
              initiallyExpanded: true, //默认是否展开
              children: <Widget>[
                cang_news != null
                    ? ListView.builder(
                        physics: const NeverScrollableScrollPhysics(),
                        shrinkWrap: true,
                        itemCount: cang_news.length,
                        itemBuilder: (context, index) {
                          return cang_news[index]['news'] != null
                              ? Container(
                                  margin: EdgeInsets.fromLTRB(10, 0, 10, 0),
                                  // margin: EdgeInsets.only(left: 20,right: 20,top: 10),
                                  child: InkWell(
                                      child: Column(
                                        children: <Widget>[
                                          Row(
                                            mainAxisAlignment:
                                                MainAxisAlignment.spaceBetween,
                                            crossAxisAlignment:
                                                CrossAxisAlignment.center,
                                            children: <Widget>[
                                              Container(
                                                width: MediaQuery.of(context)
                                                        .size
                                                        .width -
                                                    150,
                                                child: Text(
                                                  cang_news[index]['news']
                                                      ['title'],
                                                  style:
                                                      TextStyle(fontSize: 16.0),
                                                  maxLines: 3,
                                                  overflow:
                                                      TextOverflow.ellipsis,
                                                ),
                                              ),
                                              Container(
                                                width: 100,
                                                height: 75,
                                                margin: EdgeInsets.only(
                                                    left: 10, top: 5),
                                                child: Image.network(
                                                  cang_news[index]['news']
                                                      ['album'],
                                                  fit: BoxFit.cover,
                                                ),
                                              ),
                                            ],
                                          ),
                                          Column(
                                            children: <Widget>[
                                              Row(
                                                children: <Widget>[
                                                  Container(
                                                    child: Text(
                                                        "${cang_news[index]['news']['from']}",
                                                        style: TextStyle(
                                                            fontSize: 13.0)),
                                                  ),
                                                  Container(
                                                    child: Text(
                                                        "${cang_news[index]['news']['createdAt'].substring(0, 10)}",
                                                        style: TextStyle(
                                                            fontSize: 13.0)),
                                                    width: 100.0,
                                                    height: 25.0,
                                                    alignment: Alignment.center,
                                                  ),
                                                ],
                                              ),
                                            ],
                                          ),
                                          Container(
                                            width: MediaQuery.of(context)
                                                .size
                                                .width,
                                            height: 1,
                                            decoration: BoxDecoration(
                                                color: Colors.grey[300]),
                                          )
                                        ],
                                      ),
                                      onTap: () {
                                        Navigator.of(context).push(
                                            MaterialPageRoute(
                                                builder: (context) =>
                                                    MessagePage(
                                                        msg: cang_news[index]
                                                            ['news'])));
                                      }),
                                )
                              : Container();
                        },
                      )
                    : Container(child: Image.asset('images/nomessage.png')),
              ],
            ),
            ExpansionTile(
              title: Text("视频"),
              backgroundColor: Colors.white,
              initiallyExpanded: false, //默认是否展开
              children: <Widget>[
                cang_video != null
                    ? ListView.builder(
                        physics: const NeverScrollableScrollPhysics(),
                        shrinkWrap: true,
                        itemCount: cang_video.length,
                        itemBuilder: (context, index) {
                          return cang_video[index]['video'] != null
                              ? Container(
                                  margin: EdgeInsets.fromLTRB(10, 0, 10, 0),
                                  // margin: EdgeInsets.only(left: 20,right: 20,top: 10),
                                  child: InkWell(
                                      child: Column(
                                        children: <Widget>[
                                          Stack(
                                            children: <Widget>[
                                              Image(
                                                image: NetworkImage(
                                                    '${cang_video[index]['video']['album']}'),
                                                height: 200,
                                                width: MediaQuery.of(context)
                                                    .size
                                                    .width,
                                                fit: BoxFit.cover,
                                              ),
                                              Positioned(
                                                top: 10,
                                                left: 10,
                                                right: 10,
                                                child: Text(
                                                  '${cang_video[index]['video']['title']}',
                                                  style: TextStyle(
                                                      color: Colors.white,
                                                      fontSize: 16,
                                                      fontWeight:
                                                          FontWeight.w600),
                                                  maxLines: 2,
                                                  overflow:
                                                      TextOverflow.ellipsis,
                                                ),
                                              ),
                                            ],
                                          ),
                                          Row(
                                            children: <Widget>[
                                              Container(
                                                padding: EdgeInsets.all(10),
                                                alignment: Alignment.centerLeft,
                                                child: Text(
                                                  '${cang_video[index]['video']['from']}',
                                                  style: TextStyle(
                                                      fontSize: 14.0,
                                                      color: Colors.black),
                                                ),
                                              ),
                                              Container(
                                                padding: EdgeInsets.all(10),
                                                alignment: Alignment.centerLeft,
                                                child: Text(
                                                  '${cang_video[index]['video']['createdAt'].substring(0, 10)}',
                                                  style: TextStyle(
                                                      fontSize: 14.0,
                                                      color: Colors.black),
                                                ),
                                              ),
                                            ],
                                          ),
                                          Container(
                                            margin: EdgeInsets.only(bottom: 5.0),
                                            width: MediaQuery.of(context)
                                                .size
                                                .width,
                                            height: 2,
                                            decoration: BoxDecoration(
                                                color: Colors.grey[400]),
                                          )
                                        ],
                                      ),
                                      onTap: () {
                                        Navigator.of(context).push(
                                            MaterialPageRoute(
                                                builder: (context) =>
                                                    VideoPage(
                                                        msg: cang_video[index]
                                                            ['video'])));
                                      }),
                                )
                              : Container();
                        },
                      )
                    : Container(child: Image.asset('images/nomessage.png')),
              ],
            ),
          ],
        ));
  }
}
